﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace QuanLyCayXanh.Controllers
{
    [Table("Tree")]
    public class StreetController : Controller
    {
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();
        //
        // GET: /Street/

        public ActionResult Index()
        {
            return View(db.Streets.ToList());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Street street)
        {
            if(ModelState.IsValid)
            {
                db.Streets.Add(street);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(street);
        }

        public ActionResult Details(int id = 0)
        {
            Street street = db.Streets.Find(id);
            if (street == null)
                return HttpNotFound();
            return PartialView(street);
        }

        public ActionResult Edit(int id = 0)
        {
            Street street = db.Streets.Find(id);
            if (street == null)
                return HttpNotFound();
            return View(street);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Street street)
        {
            if(ModelState.IsValid)
            {
                db.Entry(street).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(street);
        }
        
        [HttpPost, ActionName("Delete")]        
        public ActionResult DeleteConfirmed(int id)
        {
            Street street = db.Streets.Find(id);
            db.Streets.Remove(street);
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }

    }
}
